package net.tv90.bfmccore.game.impl;

import net.tv90.bfmccore.game.GameBase;
import net.tv90.bfmccore.game.GameManager;
import net.tv90.bfmccore.game.GameUtils;
import net.tv90.bfmccore.game.ai.AISystem;
import net.tv90.bfmccore.game.commander.TDMCommanderSystem;
import net.tv90.bfmccore.util.HUD.ImageShow;
import net.tv90.bfmccore.util.HUD.TextShow;
import net.tv90.bfmccore.util.vector.Vector2i;
import net.tv90.bfmccore.util.vector.Vector3i;
import org.bitterorange.flansmodslain.actions.Money;
import org.bitterorange.flansmodslain.actions.Point;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.Set;

import static net.tv90.bfmccore.helper.FactionHelper.getCountry2;
import static net.tv90.bfmccore.slot.SlotHelper.giveSlot;
import static net.tv90.bfmccore.util.TimeUtil.secondsToDate;

public class TDMGame extends GameBase {

    public List<Vector3i> firstFactionSpawnPointList;
    public List<Vector3i> secondFactionSpawnPointList;
    public TDMGame() {
    }

    @Override
    public void initGameSource(String mapName, String gameMode, String firstFactionName, String secondFactionName, Vector3i mainSpawnPoint, Vector3i firstFactionMainSpawnPoint, Vector3i secondFactionMainSpawnPoint, List<Vector3i> firstFactionSpawnPointList, List<Vector3i> secondFactionSpawnPointList) {
        this.mapName = mapName;
        this.gameMode = gameMode;
        this.firstFactionName = firstFactionName;
        this.secondFactionName = secondFactionName;
        this.mainSpawnPoint = mainSpawnPoint;
        this.firstFactionMainSpawnPoint = firstFactionMainSpawnPoint;
        this.secondFactionMainSpawnPoint = secondFactionMainSpawnPoint;
        this.firstFactionSpawnPointList = firstFactionSpawnPointList;
        this.secondFactionSpawnPointList = secondFactionSpawnPointList;
        if (firstFactionSpawnPointList.isEmpty() || secondFactionSpawnPointList.isEmpty()) {
            throw new RuntimeException(this.mapName + "(" + this.gameMode + ") 没有重生点坐标！");
        }
    }

    /**
     * 将一名玩家随机传送至战区，若无阵营，传送到世界出生点
     *
     * @param p 玩家
     */
    @Override
    public void teleportSinglePlayerToBattleField(Player p) {
        if (getCountry2(p).equals(firstFactionName)) {
            Vector3i vec = GameUtils.getExactSpawnPoint(p, firstFactionSpawnPointList, EnumSpawnPolicy.FURTHEST_TO_ENEMY);
            teleportSinglePlayer2(p, vec);
        } else if (getCountry2(p).equals(secondFactionName)) {
            Vector3i vec = GameUtils.getExactSpawnPoint(p, secondFactionSpawnPointList, EnumSpawnPolicy.FURTHEST_TO_ENEMY);
            teleportSinglePlayer2(p, vec);
        } else teleportSinglePlayer2(p, mainSpawnPoint);
    }

    @Override
    public void showGameInfo() {
        //System.out.println(mapName + " " + currentTime + " " + firstFactionName + ':' + firstFactionTicket+ ' ' + secondFactionName + ':' + secondFactionTicket);
        TextShow textShowTime = new TextShow(new Vector2i(-1, 10), secondsToDate(currentTime), 1.0D, "timeText", 1);
        TextShow textShowTicket = new TextShow(new Vector2i(-1, 20), firstFactionTicket + "-" + secondFactionTicket, 1.0D, "ticketText", 1);
        textShowTime.showToPlayers(firstFactionList);
        textShowTicket.showToPlayers(firstFactionList);
        textShowTime.showToPlayers(secondFactionList);
        textShowTicket.showToPlayers(secondFactionList);
        ImageShow image1 = new ImageShow(new Vector2i(390, 8), new Vector2i(15, 15), "[local]BattleField/" + firstFactionName + ".png", 1, "FAC1");
        ImageShow image2 = new ImageShow(new Vector2i(445, 8), new Vector2i(15, 15), "[local]BattleField/" + secondFactionName + ".png", 1, "FAC2");
        image1.showToPlayers(firstFactionList);
        image2.showToPlayers(firstFactionList);
        image1.showToPlayers(secondFactionList);
        image2.showToPlayers(secondFactionList);
    }

    @Override
    public void showWinHUD(Set<Player> list) {
        //TextShow time = new TextShow(new Vector2i(380,150),secondsToDate(currentTime),5.0D,"time",6);
        TextShow ticket = new TextShow(new Vector2i(360, 210), firstFactionTicket + "-" + secondFactionTicket, 5.0D, "winTicket", 10);
        ImageShow image1 = new ImageShow(new Vector2i(270, 200), new Vector2i(50, 50), "[local]BattleField/" + firstFactionName + ".png", 10, "fac11");
        ImageShow image2 = new ImageShow(new Vector2i(530, 200), new Vector2i(50, 50), "[local]BattleField/" + secondFactionName + ".png", 10, "fac22");
        ImageShow win = new ImageShow(new Vector2i(-1, 150), new Vector2i(350, 50), "[local]BattleField/win.png", 10, "winHUD");
        win.showToPlayers(list);
        ticket.showToPlayers(list);
        image1.showToPlayers(list);
        image2.showToPlayers(list);
        list.forEach(p -> {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mv tp " + p.getName() + " world");
            p.playSound(p.getLocation(), "custom.bf4_victory", 100.0F, 1.0F);
            Point.add(p, 40);
            Money.give(p, 80);
            p.sendMessage("§e[管理员]对局结束，你获得40军衔积分和80游戏币的奖励");
            if (p.hasPermission("bfmc.vip2")) {
                p.sendMessage("§e[VIP战斗增益]你额外获得10军衔和20游戏币");
                Point.add(p, 10);
                Money.give(p, 20);
            }
        });
    }

    @Override
    public void showLoseHUD(Set<Player> list) {
        TextShow ticket = new TextShow(new Vector2i(360, 210), firstFactionTicket + "-" + secondFactionTicket, 5.0D, "winTicket", 10);
        ImageShow image1 = new ImageShow(new Vector2i(270, 200), new Vector2i(50, 50), "[local]BattleField/" + firstFactionName + ".png", 10, "fac11");
        ImageShow image2 = new ImageShow(new Vector2i(530, 200), new Vector2i(50, 50), "[local]BattleField/" + secondFactionName + ".png", 10, "fac22");
        ImageShow win = new ImageShow(new Vector2i(-1, 150), new Vector2i(350, 50), "[local]BattleField/lost.png", 10, "winHUD");
        win.showToPlayers(list);
        ticket.showToPlayers(list);
        image1.showToPlayers(list);
        image2.showToPlayers(list);
        list.forEach(p -> {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mv tp " + p.getName() + " world");
            p.playSound(p.getLocation(), "custom.bf4_defeat", 100.0F, 1.0F);
            Point.add(p, 20);
            Money.give(p, 40);
            p.sendMessage("§e[管理员]对局结束，你获得20军衔积分和40游戏币的奖励");
            if (p.hasPermission("bfmc.vip2")) {
                p.sendMessage("§e[VIP战斗增益]你额外获得5军衔和10游戏币");
                Point.add(p, 5);
                Money.give(p, 10);
            }
        });
    }

    @Override
    public void showSameHUD(Set<Player> list) {
        TextShow ticket = new TextShow(new Vector2i(360, 210), firstFactionTicket + "-" + secondFactionTicket, 5.0D, "winTicket", 10);
        ImageShow image1 = new ImageShow(new Vector2i(270, 200), new Vector2i(50, 50), "[local]BattleField/" + firstFactionName + ".png", 10, "fac11");
        ImageShow image2 = new ImageShow(new Vector2i(530, 200), new Vector2i(50, 50), "[local]BattleField/" + secondFactionName + ".png", 10, "fac22");
        ImageShow win = new ImageShow(new Vector2i(-1, 150), new Vector2i(350, 50), "[local]BattleField/same.png", 10, "winHUD");
        win.showToPlayers(list);
        ticket.showToPlayers(list);
        image1.showToPlayers(list);
        image2.showToPlayers(list);
        list.forEach(p -> {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mv tp " + p.getName() + " world");
            p.playSound(p.getLocation(), "custom.bf4_defeat", 100.0F, 1.0F);
            Point.add(p, 20);
            Money.give(p, 50);
            p.sendMessage("§e[管理员]你获得20军衔积分和50游戏币的奖励");
        });
    }

    @Override
    public void showExecuteHUD(Set<Player> list) {
        TextShow ticket = new TextShow(new Vector2i(360, 210), firstFactionTicket + "-" + secondFactionTicket, 5.0D, "winTicket", 10);
        ImageShow image1 = new ImageShow(new Vector2i(270, 200), new Vector2i(50, 50), "[local]BattleField/" + firstFactionName + ".png", 10, "fac11");
        ImageShow image2 = new ImageShow(new Vector2i(530, 200), new Vector2i(50, 50), "[local]BattleField/" + secondFactionName + ".png", 10, "fac22");
        ImageShow win = new ImageShow(new Vector2i(-1, 150), new Vector2i(350, 50), "[local]BattleField/same.png", 10, "winHUD");
        win.showToPlayers(list);
        ticket.showToPlayers(list);
        image1.showToPlayers(list);
        image2.showToPlayers(list);
        list.forEach(p -> {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mv tp " + p.getName() + " world");
            p.playSound(p.getLocation(), "custom.bf4_defeat", 100.0F, 1.0F);
        });
    }

    @Override
    public String getGameInfo() {
        return "TDM " + mapName + " " + currentTime + " " + firstFactionName + ':' + firstFactionTicket + ' ' + secondFactionName + ':' + secondFactionTicket;
    }

    @Override
    public void deploy(Player p) {
        GameManager gm = GameManager.getInstance();
        gm.currentGame.teleportSinglePlayerToBattleField(p);
        GameManager.inBattleFieldPlayers.add(p);
        try {
            giveSlot(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
        p.closeInventory();
    }

    @Override
    public void onCustomStart() {
        commanderSystem = new TDMCommanderSystem();
        aiSystem = new AISystem();
    }

    @Override
    public void onCustomEnd() {

    }
}
